package com.wlc.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wlc.dto.StudentsAndProductDto;
import com.wlc.entity.Product;
import com.wlc.entity.StudentScore;
import com.wlc.mapper.ProductMapper;
import com.wlc.mapper.UserMapper;
import com.wlc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, StudentScore> implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ProductMapper productMapper;


    @Override
    public StudentScore getUserByName(String name) {
        System.out.println("进入了getUserByName方法");
        QueryWrapper<StudentScore> wrapper = new QueryWrapper<>();
        wrapper.eq("student_name",name);
        return userMapper.selectOne(wrapper);
    }

    @Override
    public IPage<StudentScore> queryUserPage(int currentPage, int pageSize) {
        System.out.println("进入了queryUserPage方法");
        Page<StudentScore> page = new Page<>(currentPage, pageSize);
        IPage<StudentScore> productIPage = userMapper.queryStudentScore(page,1);
        return productIPage;
    }

    @Override
    public StudentsAndProductDto getStudentAndProduct(String studentName, Integer productId) {
        System.out.println("进入了getStudentAndProduct方法");
        // 获取学生成绩信息
        QueryWrapper<StudentScore> wrapper = new QueryWrapper<>();
        wrapper.eq("student_name",studentName);
        StudentScore studentScore = userMapper.selectOne(wrapper);
        // 获取商品信息
        Product product = productMapper.selectById(productId);
        StudentsAndProductDto studentsAndProductDto = new StudentsAndProductDto();
        studentsAndProductDto.setStudentScore(studentScore);
        studentsAndProductDto.setProduct(product);
        return studentsAndProductDto;
    }

    @Override
    public int saveStudentScore(StudentScore studentScore) {
        System.out.println("进入了saveStudentScore方法");
        StudentScore entity = new StudentScore();
        entity.setId("id");
        entity.setStudentName("studentName");
        entity.setCourse("course");
        entity.setScore(0);
        entity.setCreateTime(LocalDateTime.of(2020, 1, 1, 0, 0, 0));
        return userMapper.insert(studentScore);
    }

}
